﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.title
{
    public partial class newsEdit01 : BaseAdmin
    {
        readonly DALNewsType theNewsType = new DALNewsType();
        readonly DALNews theNews = new DALNews();
        tbNews oNews;
        // Fields

        private void BindNewsTypeList()
        {
            DataTable table = theNewsType.GetList("").Tables[0];
            this.ddlNewTypeList.DataTextField = "typeName";
            this.ddlNewTypeList.DataValueField = "typeId";
            this.ddlNewTypeList.DataSource = table;
            this.ddlNewTypeList.DataBind();
            this.ddlNewTypeList.Items.Add(new ExtAspNet.ListItem("请选择新闻类别", "-1"));
            this.ddlNewTypeList.SelectedValue = "-1";
        }

      

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddArticle);
                this.BindNewsTypeList();

                this.BuildForm();
             
            }
        }


        void BuildForm()
        {
            if (Request.QueryString["nid"] != null)
            {
                oNews = theNews.GetModel(int.Parse(Request.QueryString["nid"]));
                this.txtTitle.Text = oNews.newsName;
                this.txtOrder.Text = oNews.newsOrder.ToString();
                this.txtAuthor.Text = oNews.author;
                int iIsNew = oNews.isNew ? 1 : 0;
                int iVisible = oNews.visible ? 1 :0;
                this.rblIsNew.SelectedValue = this.rblIsNew.Items.FindByValue(iIsNew.ToString()).Value;
                this.rblIsVisible.SelectedValue = this.rblIsVisible.Items.FindByValue(iVisible.ToString()).Value;
                if(this.ddlNewTypeList.Items.FindByValue(oNews.typeId.ToString()) != null)
                {
                    this.ddlNewTypeList.SelectedValue = oNews.typeId.ToString();
                }
            }
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["nid"] != null)
            {
                oNews = theNews.GetModel(int.Parse(Request.QueryString["nid"]));
            }
            else
            {
                oNews = new tbNews();
            }
            oNews.newsName = this.txtTitle.Text.Trim();
            oNews.newsOrder = int.Parse(this.txtOrder.Text);
            oNews.isNew = this.rblIsNew.SelectedItem.Value == "1" ? true : false;
            oNews.visible = this.rblIsVisible.SelectedItem.Value == "1" ? true : false;
            oNews.updateTime = DateTime.Now;
            oNews.author = this.txtAuthor.Text.Trim();
            oNews.typeId = int.Parse(this.ddlNewTypeList.SelectedItem.Value);
            if (Request.QueryString["nid"] != null)
            {
                theNews.Update(oNews);
            }
            else
            {
              oNews.newsId =   theNews.Add(oNews);
            }

            ExtAspNet.Alert.Show("保存成功！", "", "window.parent.location='newsEdit.aspx?nid=" + oNews.newsId.ToString() + "'");

        }

    }
}